package com.cherry.mvc.controller;

import com.cherry.mvc.domain.User;
import com.cherry.mvc.domain.User1;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@Controller
@RequestMapping(path = "/user", method = RequestMethod.POST)
public class UserParamsController {

    //1.请求参数绑定，将参数封装在实体类中，基本类型和String
    @RequestMapping(path = "/basicParams")
    public ModelAndView basicParams(User user) {
        System.out.println("获取基本参数" + user);
        return getModelAndView(user);
    }

    //2.请求参数绑定，将参数封装在实体类中，引用类型、list和map
    @RequestMapping(path = "/referenceParams")
    public ModelAndView referenceParams(User1 user) {
        System.out.println("获取引用参数" + user);
        return getModelAndView(user);
    }

    @RequestMapping(path = "/customConvertParams")
    public ModelAndView customConvertParams(Date date) {
        System.out.println("获取日期参数" + date);
        return getModelAndView(date);
    }

    //原生的API，可以混用，不影响
    @RequestMapping(value = "/getUser")
    public String findUserById(HttpServletRequest request, HttpServletResponse response, String name) {
        System.out.println("参数=" + name);
        User user = new User();
        user.setName(name);
        user.setAge(11);
        String name1 = request.getParameter("name");
        System.out.println("req参数=" + name1);
        return "user";
    }

    private ModelAndView getModelAndView(Object o) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject(o);
        modelAndView.setViewName("user");
        return modelAndView;
    }

}
